package com.adam.example.aggregation.metric;

import com.adam.example.TransportClientHolder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats;
import org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStatsAggregationBuilder;

import java.net.UnknownHostException;

public class ExtendedStatsAggregationExample {

    public static void main(String[] args) throws UnknownHostException {
        TransportClient transportClient = TransportClientHolder.getInstance().getTransportClient();

        ExtendedStatsAggregationBuilder extendedStatsAggregationBuilder = AggregationBuilders
                .extendedStats("agg").field("price");
        SearchResponse searchResponse = transportClient.prepareSearch("test_books")
                .setSize(0).addAggregation(extendedStatsAggregationBuilder).get();
        ExtendedStats extendedStats = searchResponse.getAggregations().get("agg");
        System.out.println("max=" + extendedStats.getMax());
        System.out.println("min=" + extendedStats.getMin());
        System.out.println("sum=" + extendedStats.getSum());
        System.out.println("avg=" + extendedStats.getAvg());
        System.out.println("count=" + extendedStats.getCount());
        System.out.println("stdDeviation=" + extendedStats.getStdDeviation());  //标准差
        System.out.println("sumOfSquares=" + extendedStats.getSumOfSquares());  //平方和
        System.out.println("variance=" + extendedStats.getVariance());  //方差

        transportClient.close();
    }

}
